﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Log;
using Solver.Common;
using Solver.ModelView;
using Solver;

namespace SmileSolver
{
    public enum TaskState { Play, Stop, End }
    public delegate void CompleteTaskHandler(ISolutionTask task);

    /// OBSOLETE
    /*
    public class SolutionTask : AbstractViewModel
    {

        ILogger _log;
        public SolutionTask(ILogger log)
        {
            _log = log;
        }

        private SolutionCombination _start;
        public void FirstCombination(SolutionCombination start)
        {
            _start = start;
        }

        Task _task;
        public void Start()
        {
            State = TaskState.Play;
            _task = new Task(new Action(() =>
            {
                this.SolveCombinations();
                if (OnComplete != null)
                {
                    //OnComplete(this);
                }
            }));
            _task.Start();
        }

        public void Clear()
        {
            _circle_card.Clear();
        }


        OneCard _center;
        CircleArray<OneCard> _circle_card = new CircleArray<OneCard>();
        // pripravi prvni kombinace
        public void PrepareCombination(int idx)
        {
            _center = _start[idx];
            int cnt = 0;

            for (int i = 0; i < 9; i++)
            {
                if (i == idx)
                {
                    continue;
                }
                else
                {
                    _circle_card.AddLast(_start[i]);
                }
            }
        }

        private void SolveCombinations()
        {
            List<OneCard> list = _circle_card.ToList();
            for (int pocet = 1; pocet <= 8; pocet++)
            {
                List<OneCard> rotate = list.GetRange(0, pocet);

                foreach (OneCard c in list)
                {
                    for (int rot = 0; rot < 4; rot++)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            SolutionCombination sc = GetCombination();
                            //_log.Info(sc.ToString());
                            bool check = sc.CheckSolution();
                            if (check == true)
                            {
                                Success++;
                            }
                            Solutions++;
                            double proc = 100*(double)_solutions / (double)2048;
                            Progress = (int)proc;
                            
                            Next();
                        }
                        _circle_card.Reset();
                        foreach(OneCard rot_c in rotate)
                            rot_c.Rotate();
                    }
                    c.Reset();
                }
            }
        }

        private SolutionCombination GetCombination()
        {
            int idx = 0;
            SolutionCombination new_solution = new SolutionCombination();
            List<OneCard> list = _circle_card.ToList();
            foreach (OneCard c in list)
            {
                try
                {
                    if (idx == 4)
                    {
                        new_solution.Add(_center);
                        new_solution.Add(c);
                    }
                    else
                    {
                        new_solution.Add(c);
                    }
                }
                catch (Exception e)
                {
                    _log.Error(e.Message);
                }
                idx++;
            }

            return new_solution;
        }

        private void Next()
        {
            _circle_card.RotateRight();
            //OneCard last = _circle_card.ToList().Last();
        }

        // EVENTS
        public event CompleteTaskHandler OnComplete;

        //PROPERTIES
        public SolutionCombination Combination { get { return _start; } }


        private int _progress = 0;
        public int Progress
        {
            get { return _progress; }
            set
            {
                _progress = value;
                OnPropertyChanged("Progress");
            }
        }

        TaskState _state = TaskState.Stop;
        public TaskState State
        {
            get { return _state; }
            set
            {
                _state = value;
                OnPropertyChanged("State");
                OnPropertyChanged("StateImage");
            }
        }

        public BitmapSource StateImage
        {
            get
            {
                if (_state == TaskState.Play)
                {
                    // Create source.
                    BitmapImage bi = new BitmapImage();
                    // BitmapImage.UriSource must be in a BeginInit/EndInit block.
                    bi.BeginInit();
                    bi.UriSource = new Uri(@"/Solver;component/Images/play.png", UriKind.RelativeOrAbsolute);
                    bi.EndInit();

                    return bi;
                }
                else if (_state == TaskState.Stop)
                {
                    BitmapImage bi = new BitmapImage();
                    // BitmapImage.UriSource must be in a BeginInit/EndInit block.
                    bi.BeginInit();
                    bi.UriSource = new Uri(@"/Solver;component/Images/stop.png", UriKind.RelativeOrAbsolute);
                    bi.EndInit();

                    return bi;
                }
                else if (_state == TaskState.End)
                {
                    BitmapImage bi = new BitmapImage();
                    // BitmapImage.UriSource must be in a BeginInit/EndInit block.
                    bi.BeginInit();
                    bi.UriSource = new Uri(@"/Solver;component/Images/finish.png", UriKind.RelativeOrAbsolute);
                    bi.EndInit();

                    return bi;
                }
                else
                {
                    BitmapImage bi = new BitmapImage();
                    // BitmapImage.UriSource must be in a BeginInit/EndInit block.
                    bi.BeginInit();
                    bi.UriSource = new Uri(@"/Solver;component/Images/finish.png", UriKind.RelativeOrAbsolute);
                    bi.EndInit();

                    return bi;
                }
            }
        }

        public string Name
        {
            get;
            set;
        }

        private int _solutions = 0;
        public int Solutions
        {
            get { return _solutions; }
            set
            {
                _solutions = value;
                OnPropertyChanged("Solutions");
            }
        }

        private int _success = 0;
        public int Success
        {
            get { return _success; }
            set
            {
                _success = value;
                OnPropertyChanged("Success");
            }
        }

        public override string ToString()
        {
            return _start.ToString();
        }
    } */   
    
}
